package com.jsmile.cloud.msgcenter.zf.handler;

import java.math.BigDecimal;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliPayMessage;
import com.egzosn.pay.common.api.PayMessageHandler;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ZfbPayMessageHandler implements PayMessageHandler<AliPayMessage, AliPayService> {
    @Override
    public PayOutMessage handle(AliPayMessage aliPayMessage, Map<String, Object> map, AliPayService payService) throws PayErrorException {
        Object payId = payService.getPayConfigStorage().getAttach();
        log.info("支付宝回调：{}", JSON.toJSONString(map));
        Map<String, Object> message = aliPayMessage.getPayMessage();
        // 交易状态
        String trade_status = (String)message.get("trade_status");
        // 交易完成
        if ("TRADE_SUCCESS".equals(trade_status) || "TRADE_FINISHED".equals(trade_status)) {
            BigDecimal payAmount = new BigDecimal((String)message.get("total_fee"));
            return payService.getPayOutMessage("success", "成功");
        }

        return payService.getPayOutMessage("fail", "失败");
    }
}
